package com.syp.uuid.web.controller;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.syp.uuid.core.service.SerialServiceI;

@Controller
@RequestMapping({ "/uuid" })
public class UUIDController {

	@Autowired
	private SerialServiceI serialService;

	@RequestMapping("/next")
	@ResponseBody
	public UUIDResponse index(@RequestBody UUIDRequest request) {
		UUIDResponse response = new UUIDResponse();
		long t1 = System.currentTimeMillis();
		long serial = this.serialService.next(request.getName());
		long t2 = System.currentTimeMillis();
		
		response.setSerial(serial);
		response.setCost(t2 - t1);
		response.setTimestamp(Calendar.getInstance().getTime());
		System.err.println();
		return response;
	}

}
